<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">    
      <el-form-item label="存储过程名称" prop="procName">
        <el-input
          v-model="queryParams.procName"
          placeholder="请输入存储过程名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="isok">
        <el-select v-model="queryParams.isok" placeholder="请选择状态" style="width:140px" clearable size="small">
          <el-option
            v-for="dict in isokOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:HttpClient:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:HttpClient:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:HttpClient:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:HttpClient:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="HttpClientList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="编号" align="center" prop="id" /> -->
      <el-table-column label="线程名称" align="center" min-width="150" prop="httpSys" :show-overflow-tooltip="true"/>
      <el-table-column label="存储过程名称" align="center" min-width="150" prop="procName" :show-overflow-tooltip="true"/>
      <el-table-column label="扫描时间" align="center" width="140" prop="httpInterval"  />
      <el-table-column label="HTTP_URL" align="center" min-width="200" prop="httpUrl" :show-overflow-tooltip="true" />
      <el-table-column label="状态" align="center" prop="isok" :formatter="isokFormat" />
      <!-- <el-table-column label="操作" align="center" min-width="140" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:HttpClient:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:HttpClient:remove']"
          >删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改HTTP客户端参数对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="线程名称" prop="httpSys">
          <el-input v-model="form.httpSys" placeholder="请输入线程名称" />
        </el-form-item>
        <el-form-item label="存储过程名称" prop="procName">
          <el-input v-model="form.procName" placeholder="请输入存储过程名称" />
        </el-form-item>
        <el-form-item label="扫描时间" prop="httpInterval">
          <el-input v-model="form.httpInterval" placeholder="请输入扫描时间" />
        </el-form-item>
        <el-form-item label="HTTP_URL" prop="httpUrl">
          <el-input v-model="form.httpUrl" placeholder="请输入HTTP_URL" />
        </el-form-item>
        <!-- <el-form-item label="类型" prop="httpType">
          <el-select v-model="form.httpType" placeholder="请选择类型">
            <el-option
              v-for="dict in httpTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="状态" prop="isok">
          <el-select v-model="form.isok" placeholder="请选择状态">
            <el-option
              v-for="dict in isokOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listHttpClient, getHttpClient, delHttpClient, addHttpClient, updateHttpClient, exportHttpClient } from "@/api/system/HttpClient";

export default {
  name: "HttpClient",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // HTTP客户端参数表格数据
      HttpClientList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 0停用，1启用字典
      idOptions: [],
      // 线程名称字典
      httpSysOptions: [],
      // 存储过程名称字典
      procNameOptions: [],
      // 扫描时间字典
      httpIntervalOptions: [],
      // URL字典
      httpUrlOptions: [],
      // 1:post，2服务端字典
      httpTypeOptions: [],
      // 0停用，1启用字典
      isokOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        httpSys: null,
        procName: null,
        httpInterval: null,
        httpUrl: null,
        httpType: null,
        isok: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        httpSys: [
          { required: true, message: "线程名称不能为空", trigger: "blur" },
        ],
        procName: [
          { required: true, message: "存储过程名称不能为空", trigger: "blur" },
        ],
        httpInterval: [
          { required: true, message: "扫描时间不能为空", trigger: "blur" },
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("${column.dictType}").then(response => {
      this.idOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.httpSysOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.procNameOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.httpIntervalOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.httpUrlOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.httpTypeOptions = response.data;
    });
    this.getDicts("sys_job_status").then(response => {
      this.isokOptions = response.data;
    });
  },
  methods: {
    /** 查询HTTP客户端参数列表 */
    getList() {
      this.loading = true;
      listHttpClient(this.queryParams).then(response => {
        this.HttpClientList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // ID
    idFormat(row, column) {
      return this.selectDictLabel(this.idOptions, row.id);
    },
    // 线程名称字典翻译
    httpSysFormat(row, column) {
      return this.selectDictLabel(this.httpSysOptions, row.httpSys);
    },
    // 存储过程名称字典翻译
    procNameFormat(row, column) {
      return this.selectDictLabel(this.procNameOptions, row.procName);
    },
    // 扫描时间字典翻译
    httpIntervalFormat(row, column) {
      return this.selectDictLabel(this.httpIntervalOptions, row.httpInterval);
    },
    // URL字典翻译
    httpUrlFormat(row, column) {
      return this.selectDictLabel(this.httpUrlOptions, row.httpUrl);
    },
    // 1:类型字典翻译
    httpTypeFormat(row, column) {
      return this.selectDictLabel(this.httpTypeOptions, row.httpType);
    },
    // 状态翻译
    isokFormat(row, column) {
      return this.selectDictLabel(this.isokOptions, row.isok);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        httpSys: null,
        procName: null,
        httpInterval: null,
        httpUrl: null,
        httpType: null,
        isok: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加HTTP客户端参数";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getHttpClient(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改HTTP客户端参数";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateHttpClient(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addHttpClient(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除HTTP客户端参数编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delHttpClient(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有HTTP客户端参数数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportHttpClient(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
